package com.epro.dx.widget;

import com.epro.dx.R;
import com.epro.dx.listenner.OnSortViewItemClickListenner;
import com.weipu.common.constants.Constant;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.view.View.OnClickListener;

public class SortView extends LinearLayout implements OnClickListener {
	private String popularityStr=Constant.SORT_POPULARITY;
	
	private ImageButton populayityBtn, priceBtn, disCountBtn;

	private OnSortViewItemClickListenner listenner;

	public SortView(Context context) {
		this(context, null);
	}

	public SortView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setWillNotDraw(false);
		initView(context);
	}

	public void setOnSortViewItemClickListenner(
			OnSortViewItemClickListenner listenner) {
		this.listenner = listenner;
	}

	private String currentItemStr;

	private void initView(Context mContext) {
		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.sort_layout, this);
		populayityBtn = (ImageButton) findViewById(R.id.sort_popularity);
		priceBtn = (ImageButton) findViewById(R.id.sort_price);
		disCountBtn = (ImageButton) findViewById(R.id.sort_disCount);
		populayityBtn.setOnClickListener(this);
		priceBtn.setOnClickListener(this);
		disCountBtn.setOnClickListener(this);
	}

	/**
	 * 设置当前Popularity排序的字段
	 * @param popularityStr
	 */
	public void setPopularityStr(String popularityStr) {
		this.popularityStr = popularityStr;
	}
	
	/**
	 * 设置当前的排序类型
	 * @param currentItemStr
	 */
	public void setCurrentSortType(String currentItemStr) {
		this.currentItemStr = currentItemStr;
	}
	
	/**
	 * 获取当前排序类型
	 * @return
	 */
	public String getCurrentSortType(){
		return currentItemStr;
	}
	
	/**
	 * 设置默认排序的样式
	 */
	public void setDefaultSortStyle(){
		setCurrentSortType(popularityStr);
		populayityBtn.setImageResource(R.drawable.sort_popularity_on);
		priceBtn.setImageResource(R.drawable.sort_price);
		disCountBtn.setImageResource(R.drawable.sort_discount);
	}
	
	/**
	 * 根据sortType来设置样式
	 * @param sortType 排序类型
	 */
	public void setStyleBySortType(String sortType){
		if(Constant.SORT_POPULARITY.equals(sortType)){
			setDefaultSortStyle();
		}else if(Constant.SORT_PRICE_UP.equals(sortType)){
			priceBtn.setImageResource(R.drawable.sort_price_up_on);
			populayityBtn.setImageResource(R.drawable.sort_popularity);
			disCountBtn.setImageResource(R.drawable.sort_discount);
			setCurrentSortType(Constant.SORT_PRICE_UP);
		}else if(Constant.SORT_PRICE_DOWN.equals(sortType)){
			priceBtn.setImageResource(R.drawable.sort_price_down_on);
			populayityBtn.setImageResource(R.drawable.sort_popularity);
			disCountBtn.setImageResource(R.drawable.sort_discount);
			setCurrentSortType(Constant.SORT_PRICE_DOWN);
		}else if(Constant.SORT_DISCOUNT.equals(sortType)){
			populayityBtn.setImageResource(R.drawable.sort_popularity);
			priceBtn.setImageResource(R.drawable.sort_price);
			disCountBtn.setImageResource(R.drawable.sort_discount_on);
			setCurrentSortType(Constant.SORT_DISCOUNT);
		}
	}

	@Override
	public void onClick(View view) {
		if (null == listenner) {
			return;
		}
		switch (view.getId()) {
		case R.id.sort_popularity:
			if (popularityStr.equals(currentItemStr)) {
				return;
			}
			setCurrentSortType(popularityStr);
			populayityBtn.setImageResource(R.drawable.sort_popularity_on);
			priceBtn.setImageResource(R.drawable.sort_price);
			disCountBtn.setImageResource(R.drawable.sort_discount);
			listenner.onPopulayityClick();
			break;
		case R.id.sort_price:
			if (Constant.SORT_PRICE_UP.equals(currentItemStr)) {
				setCurrentSortType(Constant.SORT_PRICE_DOWN);
				priceBtn.setImageResource(R.drawable.sort_price_down_on);
			} else if (Constant.SORT_PRICE_DOWN.equals(currentItemStr)) {
				setCurrentSortType(Constant.SORT_PRICE_UP);
				priceBtn.setImageResource(R.drawable.sort_price_up_on);
			} else {
				setCurrentSortType(Constant.SORT_PRICE_UP);
				priceBtn.setImageResource(R.drawable.sort_price_up_on);
			}
			populayityBtn.setImageResource(R.drawable.sort_popularity);
			disCountBtn.setImageResource(R.drawable.sort_discount);
			listenner.onPriceClick();
			break;
		case R.id.sort_disCount:
			if (Constant.SORT_DISCOUNT.equals(currentItemStr)) {
				return;
			}
			populayityBtn.setImageResource(R.drawable.sort_popularity);
			priceBtn.setImageResource(R.drawable.sort_price);
			disCountBtn.setImageResource(R.drawable.sort_discount_on);
			setCurrentSortType(Constant.SORT_DISCOUNT);
			listenner.onDisCountClick();
			break;
		default:
			break;
		}
	}

	/**
	 * 设置Item是否可以点击
	 * 
	 * @param enable
	 */
	public void setSortBtnEnable(boolean enable) {
		populayityBtn.setEnabled(enable);
		priceBtn.setEnabled(enable);
		disCountBtn.setEnabled(enable);
	}
}
